package com.nox.trademe.testapp;

import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SlidingPaneLayout;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.nox.trademe.testapp.customLayout.CustomSlidingPaneLayout;
import com.nox.trademe.testapp.fragments.ListMainCategoriesFragment.CategoryListener;
import com.nox.trademe.testapp.fragments.ViewSubcategoriesFragment;
import com.nox.trademe.testapp.models.MainCategory;

public class MainActivity extends FragmentActivity implements CategoryListener{

	public static final String TAG = "MainActivity";
	private CustomSlidingPaneLayout slidingPane;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, "Activity State: onCreate()");
		super.onCreate(savedInstanceState);
        if (isOnline() == false) {
            Toast.makeText(getApplicationContext(), "Please connect to internet before using this app", Toast.LENGTH_LONG).show();
            finish();
        }
        else {
            setContentView(R.layout.activity_main);
            getActionBar().setDisplayHomeAsUpEnabled(true);
            slidingPane = (CustomSlidingPaneLayout) findViewById(R.id.slidingPane);
            slidingPane.setPanelSlideListener(new PaneListener());
            slidingPane.setCoveredFadeColor(Color.parseColor("#00ffffff"));
            slidingPane.setSliderFadeColor(Color.parseColor("#00ffffff"));
            slidingPane.setShadowResource(R.drawable.slidingpane_shadow);
            getActionBar().setTitle(R.string.maincategories_title);
            slidingPane.openPane();
            Log.v(TAG, "Activity State: onCreate() finished");
        }
	}

    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            return true;
        }
        return false;
    }

	private class PaneListener implements SlidingPaneLayout.PanelSlideListener {

		public static final String TAG = "PaneListener";
		
		@Override
		public void onPanelClosed(View view) {
			Log.v(TAG, "Panel closed");
            getActionBar().setTitle(R.string.app_name);
			getSupportFragmentManager().findFragmentById(R.id.leftPane).setHasOptionsMenu(false);
			getSupportFragmentManager().findFragmentById(R.id.rightPane).setHasOptionsMenu(true);
		}

		@Override
		public void onPanelOpened(View view) {
			Log.v(TAG, "Panel opened");
            getActionBar().setTitle(R.string.maincategories_title);
			getSupportFragmentManager().findFragmentById(R.id.leftPane).setHasOptionsMenu(true);
			getSupportFragmentManager().findFragmentById(R.id.rightPane).setHasOptionsMenu(false);
		}

		@Override
		public void onPanelSlide(View view, float arg1) {
			Log.v(TAG, "Panel sliding");
		}
		
	}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                if (slidingPane.isOpen()) {
                    slidingPane.closePane();
                }
                else {
                    slidingPane.openPane();
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

	@Override
	public void onChangeCategory(MainCategory category, boolean closePane) {
		Log.v(TAG, "onChangeCategory with category [" + category.getName() + "]");
        ViewSubcategoriesFragment right = (ViewSubcategoriesFragment)getSupportFragmentManager().findFragmentById(R.id.rightPane);
        right.onMainCategoryChange(category);
        if (closePane == true) {
            slidingPane.closePane();
        }
	}

}
